/**
 * 调用翻页方法
 */
$(function() {
	goPage(1, 12);
});
/**
 * 翻页
 * @param {Object} pno
 * @param {Object} psize
 */
var goPage = function(pno, psize) { //定义2个参数
	var itable = document.getElementById("idData"); //找到需要分页的标签元素
	var num = itable.rows.length; //表格所有行数(所有记录数)  

	var totalPage = 0; //定义一个总页数
	var pageSize = psize; //每页显示行数
	//总共分几页 
	if(num / pageSize > parseInt(num / pageSize)) { //总行数除以每页行数 
		totalPage = parseInt(num / pageSize) + 1; //条件满足加1
	} else {
		totalPage = parseInt(num / pageSize); //不满足保持不变
	}
	var currentPage = pno; //当前页数
	var startRow = (currentPage - 1) * pageSize + 1; //开始显示的行  31 
	var endRow = currentPage * pageSize; //结束显示的行   40
	endRow = (endRow > num) ? num : endRow; //  

	//遍历显示数据实现分页
	for(var i = 1; i < (num + 1); i++) {
		var irow = itable.rows[i - 1];
		if(i >= startRow && i <= endRow) {
			irow.style.display = "block";
		} else {
			irow.style.display = "none";
		}
	}
	//  var pageEnd = document.getElementById("pageEnd");
	var tempStr = "共" + num + "条记录     分" + totalPage + "页     当前第" + currentPage + "页";
	if(currentPage > 1) {
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (1) + "," + psize + ")\">首页</a>";
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (currentPage - 1) + "," + psize + ")\"><上一页</a>"
	} else {
		tempStr += "首页";
		tempStr += "<上一页";
	}

	if(currentPage < totalPage) {
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (currentPage + 1) + "," + psize + ")\">下一页></a>";
		tempStr += "<a href=\"#\" onClick=\"goPage(" + (totalPage) + "," + psize + ")\">尾页</a>";
	} else {
		tempStr += "下一页>";
		tempStr += "尾页";
	}

	document.getElementById("barcon").innerHTML = tempStr;

}

//调用全选方法	
$(function() {
	$("#qx").click(function() {
		quanxuan();
	})
});

/**
 * 点击全选按钮事件
 */
var quanxuan = function() {
	var duct = document.getElementsByName("product");
	for(var i = 0; i < duct.length; i++) {
		duct[i].checked = document.getElementById("qx").checked;
	}
}

//显示购物车方法
$(function() {
	$(".spcar").click(function() {
		//		$(".lb").html(ii);
		var hh = "";
		$(".exhibition").html("");
		//遍历数组组成字符串				
		for(var e = 0; e < cartlist.length; e++) {
			console.log(e);

			hh += "<ul class='items'><li class='shopping-img'><img src=" + cartlist[e].img + ">" + "</li>" + "<li class='shopping-name'>" + cartlist[e].name + "</li>" + "<li class='shopping-price'>" + cartlist[e].price + "</li><li class='jj'><span  data-id=" + cartlist[e].id + " class='add'>+</span><input class=number type=text value='" + cartlist[e].Number + "'><span class='del'>-</span></li>" + "<li class='amount'>" + cartlist[e].Number * cartlist[e].price + "</li></ul>";

		}

		$(".exhibition").html(hh);

		$(".cart-sp").addClass("adds");
	});

});

/**
 * 点击购物车发生的事件
 */
$(function() {
	$(".shopping-cart").click(function() {

		var inputs = $("input[name=product]:checked")
		if(inputs.length == 0) {
			alert("请选择商品！");
		} else {

			for(var i = 0; i < inputs.length; i++) { //循环遍历所选中的input标签的属性值
				if(inputs[i].checked) {
					addCart(i);

				}
				inputs[i].checked = false; //执行完获取数据后，清除选中框
			}
			alert("加入成功！");
		}
	});
});

/**
 * 获取商品信息
 */
var id = 0;
var Number = 1;
var price = 0;
var cartlist = []; //声明一个空数组
var addCart = function(i) {
	//判断是否有重复购买
	var isCart = false
	for(var z = 0; z < cartlist.length; z++) {
		if(cartlist[z].id == i) {
			isCart = true;
			cartlist[z].Number++;
			//Number++;
			console.log(Number);
		} else {
			isCart = false;
		}
	}
	if(!isCart) {
		var pirtrue = $("._goods:eq(" + i + ") img").attr("src"); //获取图片路径		拿属性	
		var name = $("._goods:eq(" + i + ") .title").html(); //获取名称				拿内容
		var price = $("._goods:eq(" + i + ") .money").html(); //获取价格				
		var goods = {} //声明一个空对象
		goods.id = i; //购买商品的id
		goods.img = pirtrue; //图片					通过对象名.属性给对象赋值  
		goods.name = name; //名称
		goods.price = price; //单价
		goods.Number = 1; //商品单个数量
		//var d ={"img":"111","name":'dddddd'}   //声明对象并赋值
		cartlist.push(goods); //添加对象数据到数组						
	}
}

/**
 * 添加商品数量
 */

$(function() {
	$(".add").click(function() {
		var x = $(".number").val();
		x = parseInt(x) + 1;
		$(".number").val("x");
	})
})

/**
 * 关闭购物车
 */
$(function() {
	$(".close-up").click(function() {
		$(".cart-sp").css("display", "none")
	})
})

/**
 * 快速查询配件
 */
$(function() {
	$(".ks").click(function() {
		$(".kscx").css("display", "block")
	})
})

/**
 * 关闭查询窗口
 */
$(function() {
	$("close-kp").click(function() {
		$(".kscx").css("display", "none")
	})
})

var goodsArray = [];

function Student(img, name, money) {
	var student = {
		"img": img,
		"name": name,
		"money": money
	}
	return student;
}

//初始化数组的信息
function init() {
	var stu1 = Student("img/shopping/tz.png", "头枕", "320.00");
	var stu2 = Student("img/shopping/zp.jpg", "帐篷", "2100.00");
	var stu3 = Student("img/shopping/xlx.png", "行李箱", "3600.00");
	var stu4 = Student("img/shopping/aqzy.jpg", "真皮座椅", "5600.00");
	var stu5 = Student("img/shopping/zsp.jpg", "雾灯装饰罩", "1200.00");
	goodsArray.push(stu1);
	goodsArray.push(stu2);
	goodsArray.push(stu3);
	goodsArray.push(stu4);
	goodsArray.push(stu5);
}

/**
 * 快速查询配件
 */
$(".cxk").blur(function() {

	Student();
	init();

	var str = $(".cxk").val();

	for(var i = 0; i < goodsArray.length; i++) {

		if(goodsArray[i].name.indexOf(str) != -1) {
			var cc = "<ul class='cxxq'><li><img class='bbcc' src=" + goodsArray[i].img + "></li><li>" + goodsArray[i].name + "</li><li>" + goodsArray[i].money + "</li></ul>"
		}
	}

	$(".xsyd").html(cc);
})